//
//  MyBankCardVC.m
//  JinFuThree
//
//  Created by 毛韶谦 on 2018/9/1.
//  Copyright © 2018年 毛韶谦. All rights reserved.
//

#import "MyBankCardVC.h"
#import "BankCardCell.h"
#import "BankCardAddCell.h"
#import "AddBankCardVC.h"
#import "ManageBankCardVC.h"
#import "MyRequestTools.h"
#import "BankInfoModel.h"

@interface MyBankCardVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) NSMutableArray *cardListArr;

@end

@implementation MyBankCardVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self getData];
}

- (void)initUI {
    
    self.title = @"银行卡管理";
    self.mainTableView.backgroundColor = ListMaoBackgroundColor;
}

//获取列表数据
-(void)getData {
    [HFQIndicationView showInView:self.view];
    [MyRequestTools getUserBankListDataSuccess:^(id  _Nullable responseObject) {
        [HFQIndicationView dismiss];
        self.cardListArr = [responseObject objectForKey:@"data"];
        [self.mainTableView reloadData];
        
    } Failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        [HFQIndicationView dismiss];
    }];
}

#pragma mark tableView代理方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.cardListArr.count + 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == self.cardListArr.count) {
        return 70;
    }else {
        return ScreenWidthMao/375*170;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.01f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == self.cardListArr.count) {
        BankCardAddCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BankCardAddCell"];
        if (!cell) {
            cell = [[BankCardAddCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BankCardAddCell"];
            
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else {
        BankCardCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BankCardCell"];
        if (!cell) {
            cell = [[BankCardCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BankCardCell"];
            
        }
        if (self.cardListArr.count != 0) {
            NSDictionary *dic = self.cardListArr[indexPath.row];
            BankInfoModel *model = [[BankInfoModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [cell updateInfoCellWithModel:model];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = ListMaoBackgroundColor;
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = ListMaoBackgroundColor;
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == self.cardListArr.count) {
        
        AddBankCardVC *vc = [[AddBankCardVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }else {
        ManageBankCardVC *vc = [[ManageBankCardVC alloc] init];
        NSDictionary *dic = self.cardListArr[indexPath.row];
        BankInfoModel *model = [[BankInfoModel alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        vc.model = model;
        [self.navigationController pushViewController:vc animated:YES];
    }
}


- (UITableView *)mainTableView {
    
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidthMao, ScreenHeight - SafeAreaTopHeight) style:UITableViewStyleGrouped];
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.contentInset = UIEdgeInsetsMake(5, 0, 20, 0);
        [self.view addSubview:_mainTableView];
    }
    return _mainTableView;
}

- (NSMutableArray *)cardListArr {
    
    if (!_cardListArr) {
        _cardListArr = [NSMutableArray array];
    }
    return _cardListArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
